import service from "@/utils/request";
import { ElMessage } from "element-plus";
import { getToken } from "@/utils/jwt";

//获取题目列表
export async function getQuestionListService(params) {
  let token = getToken();
  console.log("token" + token);
  return service
    .get("/question/list", {
      headers: {
        "Admin-Oj-b-Token": token, // 登陆令牌
      },
      params: params, // 查询参数
    })
    .then((res) => {
      if (res.code == 1000) {
        console.log(res.rows);
        return res;
      } else {
        ElMessage({
          message: res.msg,
          type: "error",
        });
        return [];
      }
    })
    .catch((error) => {
      console.log("error");
      return [];
    });
}
//获取题目详情
export async function getQuestionDetailService(questionId) {
  let token = getToken();
  let obj = {
    questionId: questionId,
  };
  console.log("token" + token);
  try {
    const res = await service.get("/question/detail", {
      headers: {
        "Admin-Oj-b-Token": token, // 登陆令牌
      },
      params: obj, // 查询参数
    });
    return res;
  } catch (error) {
    console.log("error");
    return error;
  }
}

//编辑题目
export function editQuestionService(param = {}) {
  return service({
    url: "/question/detail",
    method: "post",
    data: param,
  });
}
//删除题目
export async function deleteQuestionService(questionId) {
  let token = getToken();
  let obj = {
    questionId: questionId,
  };
  console.log("token" + token);
  try {
    const res = await service.delete("/question/delete", {
      headers: {
        "Admin-Oj-b-Token": token, // 登陆令牌
      },
      params: obj, // 查询参数
    });
    return res;
  } catch (error) {
    console.log("error");
    return error;
  }
}
//新增题目
export function addQuestionService(param = {}) {
  return service({
    url: "/question/add",
    method: "post",
    data: param,
  });
}
